package edu.maxflow;

import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.apache.hadoop.io.Writable;

public class Vertex implements Writable {

	private long vertexId;

	private List<Path> sourceExcessPaths = new ArrayList<Path>();

	private List<Path> targetExcessPaths = new ArrayList<Path>();

	private List<Edge> adjacentEdges = new ArrayList<Edge>();

	public void readFields(DataInput dataInput) throws IOException {
		sourceExcessPaths.clear();
		targetExcessPaths.clear();
		adjacentEdges.clear();

		vertexId = dataInput.readLong();

		int size = dataInput.readInt();
		if (size != 0) {
			for (int i = 0; i < size; i++) {
				Path path = new Path();
				path.readFields(dataInput);
				sourceExcessPaths.add(path);
			}
		}

		size = dataInput.readInt();
		if (size != 0) {
			for (int i = 0; i < size; i++) {
				Path path = new Path();
				path.readFields(dataInput);
				targetExcessPaths.add(path);
			}
		}

		size = dataInput.readInt();
		if (size != 0) {
			for (int i = 0; i < size; i++) {
				Edge edge = new Edge();
				edge.readFields(dataInput);
				adjacentEdges.add(edge);
			}
		}
	}

	public void write(DataOutput dataOutput) throws IOException {
		dataOutput.writeLong(vertexId);

		if (sourceExcessPaths != null) {
			dataOutput.writeInt(sourceExcessPaths.size());
			for (Path path : sourceExcessPaths) {
				path.write(dataOutput);
			}
		} else {
			dataOutput.writeInt(0);
		}

		if (targetExcessPaths != null) {
			dataOutput.writeInt(targetExcessPaths.size());
			for (Path path : targetExcessPaths) {
				path.write(dataOutput);
			}
		} else {
			dataOutput.writeInt(0);
		}

		if (adjacentEdges != null) {
			dataOutput.writeInt(adjacentEdges.size());
			for (Edge edge : adjacentEdges) {
				edge.write(dataOutput);
			}
		} else {
			dataOutput.writeInt(0);
		}
	}

	@Override
	public String toString() {
		StringBuilder builder = new StringBuilder();
		builder.append("Vertex [vertexId=");
		builder.append(vertexId);
		builder.append(", sourceExcessPaths=");
		builder.append(sourceExcessPaths);
		builder.append(", targetExcessPaths=");
		builder.append(targetExcessPaths);
		builder.append(", adjacentEdges=");
		builder.append(adjacentEdges);
		builder.append("]");
		return builder.toString();
	}

	public long getVertexId() {
		return vertexId;
	}

	public void setVertexId(long vertexId) {
		this.vertexId = vertexId;
	}

	public List<Path> getSourceExcessPaths() {
		return sourceExcessPaths;
	}

	public void setSourceExcessPaths(List<Path> sourceExcessPaths) {
		this.sourceExcessPaths = sourceExcessPaths;
	}

	public List<Path> getTargetExcessPaths() {
		return targetExcessPaths;
	}

	public void setTargetExcessPaths(List<Path> targetExcessPaths) {
		this.targetExcessPaths = targetExcessPaths;
	}

	public List<Edge> getAdjacentEdges() {
		return adjacentEdges;
	}

	public void setAdjacentEdges(List<Edge> adjacentEdges) {
		this.adjacentEdges = adjacentEdges;
	}
}
